

jQuery(function(){
  com.lolita.utils.addScript('./js/fileUpload/js/jquery.fileupload.js');
//  com.lolita.utils.addScript('./js/fileUpload/js/jquery.fileupload-process.js');
//  com.lolita.utils.addScript('./js/fileUpload/js/jquery.fileupload-image.js');
//  com.lolita.utils.addScript('./js/fileUpload/js/jquery.fileupload-ui.js');
  var $comboAnnee = jQuery('#combo-annee');
  $comboAnnee.on('change keyup',function(){
    jQuery('.saison:visible').hide();
    jQuery('.saison.saison-'+this.value).show();
  });
  
  jQuery('li.has-image').on('click',function(){
    var match_id = jQuery(this).data('match');
    com.lolita.get.match.pics(match_id);
  });
  
  var $combo = jQuery('#combo-annee');
  jQuery('li.no-match').closest('dd').each(function(){
    var $this = jQuery(this);
    var saison = $this.data('saison');
    $combo.find('option[value='+saison+']').remove()
  });  
	
//	if($combo.val()!=$combo.data('current')){
// On selection automatiquement la dernière saison visible
	$combo.val($combo.find('option:first-child').val()).trigger('change');
//	}
  
  jQuery('li.match').hover(function(event){
    var $this = jQuery(this);
    $this.stop(true,true);
    if(event.type=="mouseenter"){
      $this.switchClass("","over",500);  
    } else {
      $this.switchClass("over","",200);
    }
    
  });

  jQuery('#refresh-image').button({
     icons: {primary :'ui-icon-refresh'}
    ,text: false
  }).click(function(){
    var $this = jQuery(this);
    $this.button('option','disabled',true);
    com.lolita.utils.updateImagesCount(jQuery('#combo-annee').val(),function(rep){
      $this.button('option','disabled',false);
      if(rep.status){
        $this.fadeOut();
        window.location.reload();
      }
    });
  });
  
  jQuery('#upload-image').button({
     icons: {primary :'ui-icon-image'}
    ,text: false
  }).click(function(){
    jQuery('#upload-dialog').dialog({
       width: '80%'
      ,height: 'auto'
      ,modal: true
    });
  });
  
  var $comboMatch =jQuery('#combo-match-up');
  var $dropZone = jQuery('#dropzone');
  jQuery('#combo-annee-up').on('change keyup',function(){
    var saison_id = jQuery(this).val();
    $comboMatch.find('option').addClass('none')
    .filter('option[data-saison='+saison_id+']').removeClass('none')
    .filter(':eq(0)').attr('selected','selected');
    $comboMatch.trigger('change');
    
  }).trigger('change');
  
  $comboMatch.on('change keyup',function(){
    if($comboMatch.val()>0){
      $dropZone.switchClass('no-drop').find('h1').text('Déposez vos fichier ici');
    } else {
      $dropZone.switchClass('','no-drop').effect('pulsate').find('h1').text('Choissisez un match');
    }
      
  }).trigger('change');
  
  jQuery('.btn .add').button({
    icons : { primary : "ui-icon-folder-open"}
  }).hide().next().button({
    icons : { primary : "ui-icon-circle-arrow-n"}
  }).click(function(){
    jQuery('.a-envoyer').each(function(){
      jQuery(this).trigger('click');
    });
  }).next().button({
    icons : { primary : "ui-icon-circle-close"}
  }).click(function(){
    jQuery('.a-envoyer').remove();
  });

  var $ficLst = jQuery('#fic-lst');

  $dropZone.parent().one('switchClass',function(){
    var $this = jQuery(this).closest('.grid1');
    $this.switchClass('grid1',$this.data('swap-class'));    
  });

  var $fileUpload = jQuery('#fileupload').fileupload({
      dataType: 'json'
      ,dropZone : $dropZone
      ,progressall: function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $('#progress .bar').css(
            'width',
            progress + '%'
        );
      }
      ,add: function (e, data) {
        
        if($comboMatch.val()==0 || $comboMatch.val()==-1){
          $dropZone.trigger('change');
          $comboMatch.focus();
          com.lolita.utils.cancelEvent(e);
          return false;
        }
        
        $dropZone.parent().trigger('switchClass');
  
        data.context = $('<a/>').text('Envoyer')
          .addClass('a-envoyer')
          .button({icons : { primary : "ui-icon-pause"}})
          .prependTo($ficLst)
          .click(function () {
            data.context = $('<p/>')
            .text('Envoi en cours...')
            .switchClass('a-envoyer','encours',100)
            .replaceAll($(this))
            .button({icons : { primary : "ui-icon-transferthick-e-w"}})
            ;
            data.submit();
        }).wrap('<div/>');
      }
      ,done: function (e, data) {
          var $p = data.context;
          $p.switchClass('encours','envoye',100)
          $p.button("option", { primary: "ui-icon-check", label:"Envoyé", disabled: true} );
      }
      ,submit: function (e, data) {
         var id = $comboMatch.val();
         if(id!=0){
          data.formData = {id: id};
        } else {
          $comboMatch.focus();
          return false;
        }
      }      
  });

  jQuery(document).bind('dragover', function (e) {
      var timeout = window.dropZoneTimeout;
      
      if($dropZone.is('.no-drop'))
        return $dropZone.trigger('change');
      
      if (!timeout) {
          $dropZone.addClass('in');
      } else {
          clearTimeout(timeout);
      }
      var found = false,
          node = e.target;
      do {
          if (node === $dropZone[0]) {
              found = true;
              break;
          }
          node = node.parentNode;
      } while (node != null);
      if (found) {
          $dropZone.addClass('hover');
      } else {
          $dropZone.removeClass('hover');
      }
      window.dropZoneTimeout = setTimeout(function () {
          window.dropZoneTimeout = null;
          $dropZone.removeClass('in hover');
      }, 100);
  }).on('dragover drop',function(event){
    event.preventDefault();
  });  
  
  
});
